package com.goodfox.web.generic.area.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.goodfox.web.generic.account.entity.Account;
import com.goodfox.web.generic.area.entity.Area;
import com.goodfox.web.generic.area.service.AreaService;
import com.goodfox.web.generic.authority.service.PowerService;
import com.goodfox.web.generic.area.controller.AreaController;
import com.goodfox.web.generic.provide.exception.EntityValidateException;
import com.goodfox.web.generic.provide.service.EntityService;
import com.goodfox.web.generic.provide.util.ParameterUtil;
import com.goodfox.web.generic.provide.util.AttributeUtil;

@Controller
@RequestMapping("/area")
public class AreaController {

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(AreaController.class);

	/** 地区Service */
	@Autowired
	private AreaService areaService;
	
	/** 权限Service */
	@Autowired
	private PowerService powerService;
	
	/** 实体Service */
	@Autowired
	private EntityService entityService;
	
	/**
	 * 进入地区管理首页页面
	 * @return 目标页面
	 */
	@RequestMapping(value="goAreaIndex.shtml")
	public String goAreaIndex(HttpServletRequest request,ModelMap model){
		logger.info("进入地区管理首页");
		//判断是否有权限
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
	    	return "area/areaIndex";
	    }else{
	    	return "redirect:/login/logout.shtml";
	    }
	}
	
	/**
	 * 进入上级地区查找带回页面
	 * @return 目标页面
	 */
	@RequestMapping(value="goAreaParentLookup.shtml")
	public String goAreaParentLookup(HttpServletRequest request,ModelMap model){
		logger.info("进入上级地区查找带回页面");
		//判断是否有权限
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
	    	return "area/areaParentLookup";
	    }else{
	    	return "redirect:/login/logout.shtml";
	    }
	}

	@RequestMapping(value="areaTreeQuery.shtml")
	@ResponseBody
	public Map areaTreeQuery(HttpServletRequest request){
		Map map=new HashMap();
	    Account loginAccount=(Account)request.getSession().getAttribute("loginAccount");
	    map.put("areaList", areaService.queryAreaListByParentId(loginAccount.getActOrganization().getAreaId()));
	    map.put("curAreaId",loginAccount.getActOrganization().getAreaId());
	    return map;
	}

	/**
	 * 进入地区信息页面
	 * @param request
	 * @param model 页面回写model
	 * @return 目标页面
	 */
	@RequestMapping(value="goAreaDetail.shtml")
	public String goAreaDetail(HttpServletRequest request,ModelMap model){
		logger.info("进入地区信息页面");
		Area area=areaService.getArea(ParameterUtil.getReqParaValue(request, "areaId"));
		model.put("area",area);
		return "area/areaDetail";
	}
	
	/**
	 * 进入地区新增页面
	 * @param request
	 * @param model 页面回写model
	 * @return 目标页面
	 */
	@RequestMapping(value="goAreaAdd.shtml")
	public String goAreaAdd(HttpServletRequest request,ModelMap model){
		logger.info("进入地区新增页面");
	    Account loginAccount=powerService.havePower(request);
		if(loginAccount!=null){
			Area parentArea=areaService.getArea(ParameterUtil.getReqParaValue(request, "areaId"));
			model.put("parentArea", parentArea);
			return "area/areaAdd";
	    }else{
	    	return "redirect:/login/logout.shtml";
	    }
	}

	/**
	 * 执行地区新增
	 * @param reqMap
	 * @return
	 */
	@RequestMapping(value="areaAdd.shtml")
	@ResponseBody
	public Map areaAdd(HttpServletRequest request){
		logger.info("执行地区新增");
		Map rspMap=new HashMap();
		rspMap.put("navTabId", "areaHome");
		rspMap.put("rel", "areaShowPage");
		rspMap.put("callbackType", "");
		rspMap.put("forwardUrl", "");
		String statusCode="300";
		String message="无此权限！";
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
			try{
				Area entity=(Area)entityService.fillEntity(request,Area.class);
				areaService.addArea(entity,request.getSession().getAttribute("loginLogId").toString());
				statusCode="200";
				message="新增成功！";
			}catch(EntityValidateException evex){
				message="新增失败:"+evex.getMessage()+"！";
			}catch(Exception ex){
				logger.error("areaAdd异常：",ex);
				message="新增失败:发生异常！";
			}
		}
		rspMap.put("statusCode", statusCode);
		rspMap.put("message", message);
		logger.debug("areaAdd rspMap:"+rspMap);
		return rspMap;
	}
	
	/**
	 * 进入地区修改页面
	 * @param request
	 * @param model 页面回写model
	 * @return 目标页面
	 */
	@RequestMapping(value="goAreaModify.shtml")
	public String goAreaModify(HttpServletRequest request,ModelMap model){
		logger.info("进入地区更新页面");
	    Account loginAccount=powerService.havePower(request);
		if(loginAccount!=null){
			Area area=areaService.getArea(ParameterUtil.getReqParaValue(request, "areaId"));
			model.put("area",area);
			Area parentArea=areaService.getArea(area.getAreaIdParent());
			model.put("parentArea",parentArea);
			return "area/areaModify";
	    }else{
	    	return "redirect:/login/logout.shtml";
	    }
	}
	
	/**
	 * 执行地区修改
	 * @param reqMap
	 * @return
	 */
	@RequestMapping(value="areaModify.shtml")
	@ResponseBody
	public Map areaModify(HttpServletRequest request){
		logger.info("执行地区更新");
		Map rspMap = new HashMap();
		rspMap.put("navTabId", "areaHome");
		rspMap.put("rel", "areaShowPage");
		rspMap.put("callbackType", "");
		rspMap.put("forwardUrl", "");
		String statusCode="300";
		String message="无此权限！";
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
			try{
				Area newEny=(Area)entityService.fillEntity(request,Area.class);
				Area oldEny=areaService.getArea(newEny.getAreaId());
				areaService.modifyArea(oldEny,newEny,request.getSession().getAttribute("loginLogId").toString());
				statusCode="200";
				message="更新成功！";
			}catch(EntityValidateException evex){
				message="更新失败:"+evex.getMessage()+"！";
			}catch(Exception ex){
				logger.error("areaModify异常：",ex);
				message="更新失败:发生异常！";
			}
		}
		rspMap.put("statusCode", statusCode);
		rspMap.put("message", message);
		logger.debug("areaModify rspMap:"+rspMap);
		return rspMap;
	}

	/**
	 * 执行地区删除操作，数据物理删除
	 * @param request
	 * @param model 页面回写model
	 * @return 目标页面
	 */
	@RequestMapping(value="areaRemove.shtml")
	@ResponseBody
	public Map areaRemove(HttpServletRequest request){
		logger.info("执行地区移除");
		Map rspMap=new HashMap();
		rspMap.put("navTabId", "areaHome");
		rspMap.put("rel", "areaShowPage");
		rspMap.put("callbackType", "");
		rspMap.put("forwardUrl", "");
		String statusCode="300";
		String message="无此权限！";
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
			try{
				String areaId=ParameterUtil.getReqParaValue(request, "areaId");
				Area entity=areaService.getArea(areaId);
				boolean isExec=true;
				if(isExec){
					if(entity.getSubCount()>0){
						isExec=false;
						message="该地区中有下级地区，不能移除！";
					}
				}
				if(isExec){
					areaService.removeArea(entity,request.getSession().getAttribute("loginLogId").toString());
					statusCode="200";
					message="移除成功！";
				}
			}catch(Exception ex){
				logger.error("areaRemove异常：",ex);
				message="移除失败:发生异常！";
			}
		}
		rspMap.put("statusCode", statusCode);
		rspMap.put("message", message);
		logger.debug("areaRemove rspMap:"+rspMap);
		return rspMap;
	}
	

	/**
	 * 查询子地区列表
	 * @param request
	 * @param model 页面回写model
	 * @return 目标页面
	 */
	@RequestMapping(value="subArea.shtml")
	@ResponseBody
	public Map subArea(HttpServletRequest request){
		logger.debug("查询子地区列表");
	    Map queryMap=entityService.fillQueryMap(request,null);
		return areaService.querySubAreaList(queryMap);
	}
}
